package com.gwt.tirso.gymkhana.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.AttachEvent.Handler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwt.tirso.gymkhana.shared.CustomAnimation;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GymkhanaWeb implements EntryPoint {
	
	private Image logo = new Image("/images/logo.gif");
	
	//creation
	private Button creation = new Button("Crear Gymkhana");
	private VerticalPanel mainPanelCreat = new VerticalPanel();
	
	//contest
	private Button playBt = new Button("Jugar");
	private VerticalPanel mainPanelCont = new VerticalPanel();
	
	//registration
	private Button register = new Button("Registrarse");
	private VerticalPanel mainPanelReg = new VerticalPanel();
	
	//login
	private Login login = new Login();
	
	
	
	//shared	
	private ILoginServiceAsync loginCon;
	private VerticalPanel mainPanel = new VerticalPanel();
	
	private AbsolutePanel apCreation = new AbsolutePanel();
	private AbsolutePanel apContest = new AbsolutePanel();
	private AbsolutePanel apRegistration = new AbsolutePanel();
	
	
	private Button b = new Button("prueba");
	
	

	@Override
	public void onModuleLoad() {
		
		apCreation.setSize("48em", "4em");
		apCreation.add(creation, 2000, 10);
		
		apCreation.addStyleName("apCreation");
		
		//CustomAnimation animationCreation = new CustomAnimation(creation.getElement());
		//animationCreation.scrollTo(2, 10, 200);
		
		apContest.setSize("77em", "4em");
		apContest.add(playBt, 2000, 10);
		
		apContest.addStyleName("apContest");
		
		//CustomAnimation animationContest = new CustomAnimation(playBt.getElement());
		//animationContest.scrollTo(2, 10, 200);
		
		apRegistration.setSize("39em", "4em");
		apRegistration.add(register, 2000, 10);
		
		apRegistration.addStyleName("apRegistration");
		
		//CustomAnimation animationRegistration = new CustomAnimation(register.getElement());
		//animationRegistration.scrollTo(2, 10, 200);
		
		
		mainPanel.add(b);
		b.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				//animations
				CustomAnimation animationCreation = new CustomAnimation(creation.getElement());
				animationCreation.scrollTo(2, 10, 200);
				
				CustomAnimation animationContest = new CustomAnimation(playBt.getElement());
				animationContest.scrollTo(2, 10, 200);
				
				CustomAnimation animationRegistration = new CustomAnimation(register.getElement());
				animationRegistration.scrollTo(2, 10, 200);
				
			}
		});
		
	
		
		
		
		//shared
		//initializing login connection
		loginCon = ConnectionRpc.initRpcLogin();
		login.onModuleLoad();
		
		logo.setStyleName("mainPanelInitial");
		
		//logo
		mainPanel.add(logo);
		
		//creation
		//Button
		mainPanelCreat.add(apCreation);
		
		creation.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				loginCon.loginFromSessionServer(new AsyncCallback<Person>() {
					
					@Override
					public void onSuccess(Person result) {
						if(result == null){
							Window.alert("Para acceder a la aplicación debe iniciar sesión");
						}else{
							creationGymk();
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});		
		
		//contest
		//adding playBt to mainPanel
		mainPanelCont.add(apContest);
		//handling events on playBt
		playBt.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventClick();
			}
		});
		
		//registration
		//Button
		mainPanelReg.add(apRegistration);
		
		register.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				registration();
			}
		});
		
		mainPanelCont.addStyleName("contest");
		mainPanelCreat.addStyleName("creation");
		mainPanelReg.addStyleName("registration");
		
		RootPanel.get("mainPanel").add(mainPanel);
		RootPanel.get("contest").add(mainPanelCont);
		RootPanel.get("creation").add(mainPanelCreat);
		RootPanel.get("registration").add(mainPanelReg);		
	}

	protected void eventClick() {
		
		loginCon.loginFromSessionServer(new AsyncCallback<Person>() {
			
			@Override
			public void onSuccess(Person result) {
				if(result != null){
					contestStep1();
				}else{
					Window.alert("Para acceder a la aplicación debe iniciar sesión");
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}
		});
		
	}

	protected void contestStep1() {
		ContestStep1 contest = new ContestStep1();
		contest.onModuleLoad();
		login.onModuleDisabled();
		RootPanel.get("contest").remove(mainPanelCont);
		RootPanel.get("registration").remove(mainPanelReg);
		RootPanel.get("creation").remove(mainPanelCreat);		
	}
	
	private void registration() {
		RegistrationStep1 r = new RegistrationStep1();
		r.onModuleLoad();
		RootPanel.get("contest").remove(mainPanelCont);
		RootPanel.get("registration").remove(mainPanelReg);
		RootPanel.get("creation").remove(mainPanelCreat);			
	}
	
	private void creationGymk() {
		CreationStep1 s = new CreationStep1();
		s.onModuleLoad();
		RootPanel.get("contest").remove(mainPanelCont);
		RootPanel.get("registration").remove(mainPanelReg);
		RootPanel.get("creation").remove(mainPanelCreat);	
	}
	
}
